%% =======================================================================
% Figure F.1
% =======================================================================
clear all; clc; close all;
rng(11);
NN    = 1e6;

currentFile = mfilename( 'fullpath' );
[pathstr,~,~] = fileparts( currentFile );
addpath( fullfile( pathstr, '_figures' ) );

tt    = 1.00;
tx    = 0.80^2; 
tx1   = 1.45^2; 
teps  = 6.67^2; 
mu    = 0;

theta = mu*ones(NN,1)+sqrt(1/tt)*randn(NN,1);
ei1   = sqrt(1/tx)*randn(NN,1);
xi1   = theta+ei1;
ei2   = sqrt(1/tx)*randn(NN,1);
eps   = sqrt(1/teps)*randn(NN,1);
xi2   = theta+ei2;

x_values = [-2:0.01:2];

% OVC
v     = tx1/(tt+tx1);
vs    = tx/(tt+tx);
ty    = vs^2;

wx    = tx1/(tt+2*tx1+ty*teps);
wy    = ty*teps/(tt+2*tx1+ty*teps);

y     = (v/vs).*theta+(1/vs).*eps;

fi1  = v.*xi1+(1-v).*mu;
fi2  = wx.*xi1+wx.*xi2+wy.*y+(1-2*wx-wy).*mu;

f1   = v.*theta+(1-v).*mu;
f2   = wx.*theta+wx.*theta+wy.*y+(1-2*wx-wy).*mu;

error_2 = fitdist(theta-fi2, 'Normal');
error_1 = fitdist(theta-f1, 'Normal');
descr   = fitdist(fi2-f2, 'Normal'); 

error_2 = pdf(error_2, x_values);
error_1 = pdf(error_1, x_values);
descr   = pdf(descr, x_values); 

% RE
v     = tx/(tt+tx);
vs    = tx/(tt+tx);
ty    = vs^2;

wx    = tx/(tt+2*tx+ty*teps);
wy    = ty*teps/(tt+2*tx+ty*teps);

y     = (v/vs).*theta+(1/vs).*eps;

fi1  = v.*xi1+(1-v).*mu;
fi2  = wx.*xi1+wx.*xi2+wy.*y+(1-2*wx-wy).*mu;

f1   = v.*theta+(1-v).*mu;
f2   = wx.*theta+wx.*theta+wy.*y+(1-2*wx-wy).*mu;

error_2_re = fitdist(theta-fi2, 'Normal');
error_1_re = fitdist(theta-f1, 'Normal');
descr_re   = fitdist(fi2-f2, 'Normal'); 

error_2_re = pdf(error_2_re, x_values);
error_1_re = pdf(error_1_re, x_values);
descr_re   = pdf(descr_re, x_values); 


% FIGURE
figure(1)
hold on
plot(x_values,descr_re,'k--','LineWidth',2)
plot(x_values,descr,'k','LineWidth',2)
xlabel('Individual forecasts: deviation from consesus ($f_{i2}-f_2$)','Interpreter','Latex','FontSize',18)
h1= legend('Rational expectations','Overconfidence')%,'$Var_{dc|dy>0}/Var_{dc|dy<0}$ (rhs)'
set(h1,'Interpreter','Latex','FontSize',18,'Location','SouthOutside','Orientation','Horizontal','FontSize',16)
%xlabel('Individual forecast errors ($f_{i2}-\theta$)','Interpreter','Latex','FontSize',18)
%saveas(gcf,'_figures/figure_e1_2','epsc')
set(gcf, 'PaperOrientation', 'landscape')
set(gcf,'units','normalized','outerposition',[0 0 1.5 1.5])
print(figure(1),'-fillpage','_figures/figure_f1_1','-dpdf','-r0')

close all; 

figure(1)
hold on
plot(x_values,error_2_re,'k--','LineWidth',2)
plot(x_values,error_2,'k','LineWidth',2)
xlabel('Individual forecast errors ($f_{i2}-\theta$)','Interpreter','Latex','FontSize',18)
h1= legend('Rational expectations','Overconfidence')%,'$Var_{dc|dy>0}/Var_{dc|dy<0}$ (rhs)'
set(h1,'Interpreter','Latex','FontSize',18,'Location','SouthOutside','Orientation','Horizontal','FontSize',16)
%xlabel('Individual forecast errors ($f_{i2}-\theta$)','Interpreter','Latex','FontSize',18)
%saveas(gcf,'_figures/figure_e1_2','epsc')
set(gcf, 'PaperOrientation', 'landscape')
set(gcf,'units','normalized','outerposition',[0 0 1.5 1.5])
print(figure(1),'-fillpage','_figures/figure_f1_2','-dpdf','-r0')

close all; 

figure(1)
hold on
plot(x_values,error_1_re,'k--','LineWidth',2)
plot(x_values,error_1,'k','LineWidth',2)
xlabel('Error in consensus forecasts ($f_{1}-\theta$)','Interpreter','Latex','FontSize',18)
h1= legend('Rational expectations','Overconfidence')%,'$Var_{dc|dy>0}/Var_{dc|dy<0}$ (rhs)'
set(h1,'Interpreter','Latex','FontSize',18,'Location','SouthOutside','Orientation','Horizontal','FontSize',16)
%xlabel('Individual forecast errors ($f_{i2}-\theta$)','Interpreter','Latex','FontSize',18)
%saveas(gcf,'_figures/figure_e1_2','epsc')
set(gcf, 'PaperOrientation', 'landscape')
set(gcf,'units','normalized','outerposition',[0 0 1.5 1.5])
print(figure(1),'-fillpage','_figures/figure_f1_3','-dpdf','-r0')

close all; 
